<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\WechatController;
use App\Models\Apply;
use App\Models\Permission;
use App\Models\Role;
use App\Service\ApplyService;
use Cache;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\ApplyRequest;
use Illuminate\Support\Facades\Redis;


/**
 * 报名控制器
 * Class ApplyController
 * @package App\Http\Controllers\Admin
 *
 */
class ApplyController extends Controller
{
    private $apply;

    function __construct(ApplyService $apply)
    {
        // 自定义报名中间件
        $this->middleware('check.permission:apply');
        $this->apply = $apply;
    }

    /**
     * 报名列表
     * /admin/apply
     * @author linyi
     *
     * @return [type]     [description]
     */
    public function index()
    {
        //视图文件：resources/views/admin/apply/list.blade.php
        return view('admin.apply.list');
    }

    /**
     * datatables获取数据
     * @author linyi
     *
     * @return [type]     [description]
     */
    public function ajaxIndex()
    {
        $responseData = $this->apply->ajaxIndex();
        return response()->json($responseData);
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show($id)
    {
        $apply = $this->apply->getApplyById($id);
        if (getUser()->can('stage.' . $apply->stage) && !in_array($apply->status, [Apply::STATUS_PASS, Apply::STATUS_REFUSE])) {
            $apply->canReview = true;
        } else {
            $apply->canReview = false;
        }

        $reviews = $apply->reviews()->with('admin')->get();
        foreach ($reviews as &$item) {
            unset($item['admin']['openid']);
            unset($item['admin']['dingid']);
        }
        return view('admin.apply.show')->with(compact('apply', 'reviews'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->apply->destroyApply($id);
        return redirect('admin/apply');
    }

    /**
     * 审核
     * /admin/apply/review
     */
    public function review()
    {
        $responseData = $this->apply->review();
        flash_info($responseData['error_code']==0, $responseData['error_msg'], $responseData['error_msg']);
        return redirect('admin/apply/'.request('id'));
    }

    /**
     * 获取以前的审核内容
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function getReview($id)
    {
        $apply = $this->apply->getApplyById($id);
        $r = $apply->reviews()->with('admin')->get();
        foreach ($r as &$item) {
            unset($item['admin']['openid']);
            unset($item['admin']['dingid']);
        }
        return response()->json($r);
    }

    /**
     * 首页统计
     * /admin/apply/stat
     * method: GET
     */
    public function stat()
    {
        $responseData = $this->apply->stat();
        return response()->json($responseData);
    }
}
